//促销控制器
var PromotionIndexCtrl = ['$rootScope', '$scope', '$location', 'ngDialog', 'Promotion', 'GoodsCart', function ($rootScope, $scope, $location, ngDialog, Promotion, GoodsCart) {
        //$scope.list = [
        //	{'class': 'select hot', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': 'overdue', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': 'select', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': '', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': '', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': '', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': 'hot', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': '', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': '', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': '', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': '', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'},
        //	{'class': '', 'name': '买一送一', 'desc': '购买任意秋装即可送同价商一件品', 'time': '2015.08.01-2015.10.01'}
        //];


        //baison_app.api.alert({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON],msg:11});
        //$scope.list = [];

        var window_id;

        //购物车列表
        if (!$rootScope.goods_cart) {
            GoodsCart.init();
        }

        var _select_promotions = [];
        $rootScope.goods_cart.select_promotions.forEach(function (e) {
            _select_promotions.push(e.promotion_rule_id);
        });
        
        Promotion.list().then(function (ret) {
            $scope.list = {};
            $.each($rootScope.goods_cart.today_promotions, function (i, n) {
                var s = n;
                Promotion.getPromotionOptions_by_promo_id(n.promotion_rule_id).then(function(ret){
					s.vip_price = ret[0].vip_price;
					s.customer_price = ret[0].customer_price;
					s.customer_num = ret[0].customer_num;
					s.reduce_price = ret[0].reduce_price;
					s.gift_total_price = ret[0].gift_total_price;
					s.gift_total_num = ret[0].gift_total_num;
					s.rebate = ret[0].rebate;
					s.promotion_rule_id = n.promotion_rule_id;
	                s.class = 'hot';
	                s.desc = n.remark;
	                s.time = n.end_time;
	                s.name = n.name;
	                
	
	                if (-1 != _select_promotions.indexOf(n.promotion_rule_id)) {
	                    s.class = 'select hot';
	                }
	                $scope.list[i] = s;
				});  
            });
        });
        
//         Promotion.get_available_promotion().then(function (ret) {
//             $scope.list = {};
//             $.each($rootScope.goods_cart.available_promotions, function (i, n) {
//                 var s = n;
//                 //Promotion.getPromotionOptions_by_promo_id(n.promotion_rule_id).then(function(ret){
// //				s.vip_price = ret[0].vip_price;
// //				s.customer_price = ret[0].customer_price;
// //				s.customer_num = ret[0].customer_num;
// //				s.reduce_price = ret[0].reduce_price;
// //				s.gift_total_price = ret[0].gift_total_price;
// //				s.gift_total_num = ret[0].gift_total_num;
// //				s.rebate = ret[0].rebate;
// //				s.promotion_rule_id = n.promotion_rule_id;
//                 s.class = 'hot';
//                 s.desc = n.remark;
//                 s.time = n.end_time;
//                 s.name = n.name;
                

//                 if (-1 != _select_promotions.indexOf(n.promotion_rule_id)) {
//                     s.class = 'select hot';
//                 }
//                 if ($rootScope.goods_cart.available_promotions[i].promotion_type != 5) {
//                     $scope.list[i] = s;
//                 }
                
// 				//});  
//             });
//         });

        $scope.viewDetail = function (detail) {
            window_id = ngDialog.open({closeByDocument: false,
                template: './html/promotion/prom_detail.html',
                disableAnimation: true,
                scope: $scope,
                trapFocus: false,
                controller: ['$scope', function ($scope) {
                        $scope.detail = detail;
                    }]
            });
        };
          //获取赠送物品
        var get_gift_goods = function (detail) {
            var pid = detail.promotion_rule_id;
            //赠送数量
            var num = detail.gift_total_num;           //赠品所在促销的详情
            $scope.gift_detail = detail;
            //所有赠品信息
            $scope.gift_goods = [];
            //选中赠品列表
            $scope.gift_list = {};
            //赠送数量
            $scope.giftnum = num;
            Promotion.get_gift_goods(pid).then(function (ret) {
                //赠品数量
                var giftnum = ret.length;
                if (giftnum != 0) {
                    if (giftnum <= num) {
                        detail.gift_list = ret;
                        GoodsCart.add_current_promotion(detail);
                    } else if (giftnum > num) {
                        gift_id = ngDialog.open({closeByDocument: false,
                            template: './html/cashier/dialog/gift_prom.html',
                            disableAnimation: true,
                            scope: $scope,
                            controller: ['$scope', function ($scope) {
                                    $scope.gift_goods.gift_list = ret;
                                }]
                        });
                    }
                }

            });
        };
        //选择赠送物品
        $scope.giftgoods_on_change = function (gift, index) {
            var pid = $scope.gift_detail.promotion_rule_id;
            var num = $scope.gift_detail.gift_total_num;
            if (gift.selected === true) {
                if ($scope.giftnum <= 0) {
                    baison_app.api.toast({msg: $rootScope.language.MSG_CANT_SELECT_OTHER_GOODS}); //'不能再选择其他商品'
                    gift.selected = false;
                    return;
                }
                $scope.giftnum = $scope.giftnum - 1;
                $scope.gift_list[gift.sku] = gift;
            } else {
                $scope.giftnum = $scope.giftnum + 1;
                delete $scope.gift_list[gift.sku];
            }
        };
        //确定赠品
        $scope.close_gift = function () {
            var pid = $scope.gift_detail.promotion_rule_id;
            $scope.gift_detail.gift_list = $scope.gift_list;
            GoodsCart.add_current_promotion($scope.gift_detail);
            ngDialog.close(gift_id);
            $scope.gift_detail = {};
        };

//点击立即参与
        $scope.add_select_promotions = function (detail) {
			
            if (!$rootScope.goods_cart) {
                GoodsCart.init();
            }
            
            //if (detail.promotion_type == 1 || detail.promotion_type == 3) {//满额赠件
             //   get_gift_goods(detail);
            //} else {
                GoodsCart.add_select_promotion(detail);
            //}
            
            ngDialog.close(window_id);
            $location.path('/cashier/index');
        }
    }];
PromotionIndexCtrl.$injector = ['$rootScope', '$scope', '$location', 'ngDialog', 'Promotion', 'GoodsCart'];
